from selenium import webdriver
import time
import requests
import re
import os


pageUrl = 'https://item.jd.com/100009505280.html'
# 1 背景图片模式 2 img 懒加载模式
imgType = 1


match = re.search(r'\/(\d+)\.htm', pageUrl)
googsId = match.group(1)

fp = webdriver.FirefoxProfile()

options = webdriver.FirefoxOptions()
options.add_argument('-headless')
options.add_argument('--disable-gpu')

driver = webdriver.Firefox(executable_path="D:\service\geckodriver.exe", firefox_profile=fp, options=options)

driver.get(pageUrl)

baseDir = "D:\\JdImg"
if not os.path.exists(baseDir):
    os.mkdir(baseDir)
storeDir = baseDir + "\\" + googsId
if not os.path.exists(storeDir):
    os.mkdir(storeDir)

imageHeader = {
    'accept' : 'image/webp,image/apng,image/*,*/*;q=0.8',
    'accept-language' : 'zh-CN,zh;q=0.9',
    'cache-control' : 'no-cache',
    'referer' : pageUrl,
    'sec-fetch-mode' : 'no-cors',
    'sec-fetch-site' : 'cross-site',
    'user-agent' : 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36',
}


def downImg(i, imgUrl):
    print(imageUrl)
    ext = imageUrl[imageUrl.rfind('.'):]
    imageSource = requests.get(imageUrl, headers=imageHeader)
    with open(storeDir + "\\" + str(i) + ext, 'wb') as f:
        f.write(imageSource.content)
    print(str(i) + '下载OK')


topImages = driver.find_element_by_css_selector("div#spec-list").find_elements_by_css_selector("img")

i = 0
for image in topImages:
    i += 1
    imageUrl = 'https://img10.360buyimg.com/n1/' + image.get_attribute('data-url')
    downImg('t' + str(i), imageUrl)

if imgType == 1:
    images = driver.find_element_by_css_selector(
        "div#J-detail-content > div.ssd-module-wrap").find_elements_by_css_selector("div.ssd-module")
    i = 0
    for image in images:
        i += 1
        imageUrl = image.value_of_css_property("background-image")
        imageUrl = imageUrl.replace('url("', '').replace('")', '')
        downImg(i, imageUrl)
elif imgType == 2:
    driver.execute_script("arguments[0].scrollIntoView();", driver.find_element_by_css_selector("div#activity_footer"))  # 拖动到可见的元素去
    time.sleep(3)
    images = driver.find_element_by_css_selector("div#J-detail-content").find_elements_by_css_selector("img")
    i = 0
    for image in images:
        i += 1
        imageUrl = image.get_attribute('src')
        downImg(i, imageUrl)

driver.close()